package io.renren.hd.controller;

import io.renren.hd.pojo.GenConfig;
import io.renren.hd.service.GenCodeService;
import io.renren.utils.R;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author liuzhen
 * @date 2022/4/2313:57
 * @Description:
 */
@RestController
@RequestMapping("/generator")
public class GenCodeController {

     @Autowired
     private GenCodeService genCodeService;

    /**
     * 生成代码
     */
    @RequestMapping("/code")
    public void code(@RequestBody List<GenConfig> configList,HttpServletResponse response) throws IOException {
        byte[] data = genCodeService.generatorCode(configList);

        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"code.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");

        IOUtils.write(data, response.getOutputStream());
    }


    /**
     * 预览代码
     */
    @RequestMapping("/view")
    public R codeView(@RequestBody GenConfig genConfig)  {
        Map<String,Object> map= genCodeService.generatorCodeView(genConfig);


        return R.ok().put("data",map);
    }

}
